package com.vit.dao;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import com.vit.domain.User;

/**
 * DAO Impl for User
 * @author i80260
 *
 */
public class UserDAOImpl implements UserDAO
{
	
private NamedParameterJdbcTemplate namedParamJdbcTemplate;
	
	public User saveUser(User user)
	{
		System.out.println("User object saving into database.");
		Map addParams = new HashMap();
		
		//Insert into Contact Table
		String addContactSql = "INSERT INTO CONTACT(ADDRESS,PHONE) VALUES (:ADDRESS,:PHONE)";
		addParams.put("ADDRESS", user.getContact().getAddress());
		addParams.put("PHONE", user.getContact().getPhone());
		namedParamJdbcTemplate.update(addContactSql,addParams);
		
		//Insert into User table
		String addUSERSql = "INSERT INTO USER(FIRSTNAME,LASTNAME,ID) VALUES (:FIRSTNAME,:LASTNAME,:ID)";
		addParams.put("FIRSTNAME", user.getFirstName());
		addParams.put("LASTNAME", user.getLastName());
		addParams.put("ID", user.getId());
		namedParamJdbcTemplate.update(addUSERSql,addParams);
		System.out.println("User object saved into database.");
		return user;
	}
	public void setDataSource(DataSource dataSource) {
		this.namedParamJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
	}
}
